<template>
  <div class="login-container">
    <el-form class="myform" :model="form" :rules="rules" ref="form">
      <h3 class="title">
        <img src="@/assets/common/login-logo.png" alt="" />
      </h3>
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          show-password
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          class="login-btn"
          @click="login"
          :loading="loading"
          >登录</el-button
        >
      </el-form-item>
      <a href="">还没有账号？立即注册</a>
    </el-form>
  </div>
</template>

<script>
import { loginApi } from '@/api/user'
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              const bool = reg.test(value)
              bool ? callback() : callback(new Error('手机号不合法'))
            },
            trigger: 'blur'
          }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    ...mapActions({
      LOGIN: 'user/login'
    }),
    login () {
      this.loading = true
      //全局表单验证
      this.$refs.form.validate(async result => {
        if (result) {
          try {
            await this.LOGIN(this.form)
            const _redirect = this.$route.query._redirect
            if(_redirect){
              this.$router.push(_redirect)
            }else{
              this.$router.push('/')
            }
            this.$message.success('登录成功')
          } catch (error) {
            console.log(error)
          }
          this.loading = false
        } else {
          console.log('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  background: url('~@/assets/common/login.jpg');
  background-position: center;
  height: 100%;
  box-sizing: border-box;
  .myform {
    width: 520px;
    height: 400px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 40px;
    padding-top: 0;

    .el-form-item {
      // border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(255, 255, 255, 0.7); // 输入登录表单的背景色
      border-radius: 5px;
      color: #454545;
      border: 0;
      .el-input__inner {
        height: 46px;
        color: #68b0fe;
        background: rgba(255, 255, 255, 0.7);
      }
      .login-btn {
        width: 100%;
        height: 50px;
        font-size: 24px;
      }
    }
    a {
      color: #fff;
      display: block;
      width: 160px;
      margin: 0 auto;
    }
  }
}
</style>
